<?php

namespace App\Http\Controllers\Admin\Collection;

use App\Exceptions\CollectionException;
use App\Services\Collection\CollectionFileService;
use Exception;
use Illuminate\Http\JsonResponse;

class CollectionFileController extends BaseCollectionController
{
    private CollectionFileService $collectionFileService;

    public function __construct()
    {
        parent::__construct();
        $this->collectionFileService = $this->loadService('Collection\CollectionFileService');
    }

    /**
     * 添加藏品文件
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function create(): JsonResponse
    {
        $rule = [
            'collectionId' => 'required|integer|min:1',
            'type'         => 'required|integer|min:1',
            'fileUrl'      => 'required|string|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->collectionFileService->create($this->params);
        return outputJsonResult();
    }

    /**
     * 更新藏品文件
     *
     * @return JsonResponse
     * @throws CollectionException
     */
    public function update(): JsonResponse
    {
        $rule = [
            'id'      => 'required|integer|min:1',
            'type'    => 'required|integer|min:1',
            'fileUrl' => 'required|string|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->collectionFileService->update($this->params);
        return outputJsonResult();
    }

    /**
     * 删除藏品文件
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function delete(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->collectionFileService->delete($this->params);
        return outputJsonResult();
    }

    /**
     * 批量删除藏品文件
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function deleteByIds(): JsonResponse
    {
        $rule = [
            'ids' => 'required|array|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->collectionFileService->deleteByIds($this->params);
        return outputJsonResult();
    }

    /**
     * 藏品文件列表
     *
     * @return JsonResponse
     * @throws CollectionException
     */
    public function list(): JsonResponse
    {

        $rule = [
            'collectionId' => 'required|integer|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $list = $this->collectionFileService->list($this->params);
        return outputJsonResult($list);
    }

    /**
     * 藏品文件详情
     *
     * @return JsonResponse
     * @throws CollectionException
     */
    public function detail(): JsonResponse
    {

        $rule = [
            'id' => 'required|integer|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $detail = $this->collectionFileService->detail($this->params);
        return outputJsonResult($detail);
    }
}
